# -*- coding: utf-8 -*-
import requests
import json
import os

FILENAME = 'pdfFile'
CURRENT_PATH = os.path.dirname(__file__)

def find_stock_flvyjs():
    url = 'http://120.26.93.104/api/stock/urlList'
    dataDic = {'length':100,'regex':'法律意见书'}
    data = requests.get(url,params=dataDic)
    urlList = json.loads(data.content)
    count = 0
    for pdfkey in urlList.get('urlList'):
        count += 1
        pdfDic = {'key':pdfkey}
        pdfInfoUrl = 'http://121.199.8.121:7300/api/download'
        pdfInfo = requests.get(pdfInfoUrl,params=pdfDic)
        downloadUrl = json.loads(pdfInfo.content).get('downloadUrl')
        pdfcontent = requests.get(downloadUrl).content
        pdfPath = os.path.join(CURRENT_PATH,FILENAME)
        with open(pdfPath +'/'+ pdfkey,'w') as f:
            f.write(pdfcontent)
        print 'downloads No.%d pdf'%count

def find_stock_fkyj():
    url = 'http://120.26.93.104:8000/api/stock/xinsanban/urlList'
    data = requests.get(url)
    urlList = json.loads(data.content)
    count = 0
    for item in urlList.get('itemList'):
        count += 1
        pdfDic = {'key':item.get('fileKey')}
        pdfInfoUrl = 'http://121.199.8.121:7300/api/download'
        pdfInfo = requests.get(pdfInfoUrl,params=pdfDic)
        downloadUrl = json.loads(pdfInfo.content).get('downloadUrl')
        pdfcontent = requests.get(downloadUrl).content
        pdfPath = os.path.join(CURRENT_PATH,FILENAME)
        with open(pdfPath +'/'+ (item.get('title')+'.pdf'),'w') as f:
            f.write(pdfcontent)
        print 'downloads No.%d pdf'%count

if __name__=='__main__':
    # find_stock_flvyjs()
    find_stock_fkyj()